package com.machine.filling.function.main.erweima;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseFragment;
import com.machine.filling.db.dao.ExceptionOrderDao;
import com.machine.filling.db.dao.TempOrderDao;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.function.main.PayRes.PayResFragment;
import com.machine.filling.function.main.payChoose.PayChooseFragment;
import com.machine.filling.net.ws.response.WsResCheckOrderPayState;
import com.machine.filling.net.ws.response.WsResGetErWeiMa;
import com.machine.filling.util.Common;
import com.machine.filling.util.HexUtils;
import com.machine.filling.util.QRCode;

/**
 * Created by Administrator on 2018/4/19 0019.
 *
 * @description 二维码扫码支付
 */
public class ErWeiMaFragment extends BaseFragment<ErWeiMaContract.Presenter> implements ErWeiMaContract.View, View.OnClickListener {
    public ErWeiMaFragment() {
    }

    private INextFragment INextFragment;
    private String portNum;
    private Button bt_back;
    private TextView tv_notice;
    private TextView tv_bottom;
    private TextView tv_price;
    private TextView tv_notice2;
    private ImageView iv_img;
    private ImageView iv_type;
    private String payType;
    private String amount;

    public ErWeiMaFragment(Intent mIntent, String portNum) {
        super(mIntent);
        this.portNum = portNum;
    }

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_fg_erweima, null);
        tv_notice = view.findViewById(R.id.tv_notice);
        bt_back = view.findViewById(R.id.bt_back);
        tv_bottom = view.findViewById(R.id.tv_bottom);
        tv_price = view.findViewById(R.id.tv_price);
        tv_notice2 = view.findViewById(R.id.tv_notice2);
        iv_img = view.findViewById(R.id.iv_img);
        iv_type = view.findViewById(R.id.iv_type);
        bt_back.setOnClickListener(this);
        return view;
    }

    @Override
    public void initDatas() {
        payType = fragmentIntent.getStringExtra("payType");
        if (Common.IS_EXCETPION_ORDER_USE) {
            amount = HexUtils.formatDouble(Double.parseDouble(ExceptionOrderDao.getInstance().queryByPortNum(portNum).getAmount()), 2);
        } else {
            amount = HexUtils.formatDouble(Double.parseDouble(TempOrderDao.getInstance().queryByPortNum(portNum).getPayTotalMoney()), 2);
        }
        tv_price.setText("支付金额: " + amount + "元");
        switch (payType) {
            case Common.PAY_MODE_ZHIFUBAO:
                tv_notice.setText("请使用支付宝扫一扫功能");
                tv_bottom.setText("支付宝支付");
                if (Common.IS_EXCETPION_ORDER_USE) {
                    ExceptionOrderDao.getInstance().upadteOrderPayMode(portNum, Common.PAY_MODE_ZHIFUBAO);
                } else {
                    TempOrderDao.getInstance().upadteOrderPayMode(portNum, Common.PAY_MODE_ZHIFUBAO);
                }
                Glide.with(mContext).load(R.drawable.pay_zhifubao).into(iv_type);
                break;
            case Common.PAY_MODE_WEIXIN:
                tv_notice.setText("请使用微信扫一扫功能");
                tv_bottom.setText("微信支付");
                if (Common.IS_EXCETPION_ORDER_USE) {
                    ExceptionOrderDao.getInstance().upadteOrderPayMode(portNum, Common.PAY_MODE_WEIXIN);
                } else {
                    TempOrderDao.getInstance().upadteOrderPayMode(portNum, Common.PAY_MODE_WEIXIN);
                }
                Glide.with(mContext).load(R.drawable.pay_weixin).into(iv_type);
                break;
        }
        mPresenter = new ErWeiMaPresenter(mContext, this);
        mPresenter.getErWeiMa();

    }

    public void setINextFragment(INextFragment INextFragment) {
        this.INextFragment = INextFragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_back:
                mPresenter.stopCycle(true);
                INextFragment.replaceNext(new Intent(), PayChooseFragment.class.getName());
                break;
        }
    }

    @Override
    public String getOrderNum() {
        if (Common.IS_EXCETPION_ORDER_USE) {
            return ExceptionOrderDao.getInstance().queryByPortNum(portNum).getOrderNum();
        } else {
            return TempOrderDao.getInstance().queryByPortNum(portNum).getOrderNum();
        }
    }

    @Override
    public String getPayType() {
        return payType;
    }

    @Override
    public void showGetErWeiMaResultSuccess(WsResGetErWeiMa bean) {
        tv_notice2.setText("扫描二维码支付");
        Bitmap bitmap = QRCode.createQRCode(bean.getQrCodeUrl());
        iv_img.setImageBitmap(bitmap);
        // TODO: 2018/4/22 0022 无限循环 请求服务器获取支付状态
        mPresenter.getCyclePayState();
    }

    @Override
    public void showGetErWeiMaResultFailed(String errorCode, String errorMsg) {
        tv_notice2.setText("二维码获取失败");
    }

    /**
     * 显示支付成功
     *
     * @param bean
     */
    @Override
    public void showPayResSuccess(WsResCheckOrderPayState bean) {
        if ("1".equals(bean.getState())) {
            Intent intent = new Intent();
            intent.putExtra("payType", bean.getPayType());//支付方式： 1支付宝 2微信 3现金 4油卡
            intent.putExtra("amount", bean.getAmount());//加油的实际付款金额
            INextFragment.replaceNext(intent, PayResFragment.class.getName());
            if (Common.IS_EXCETPION_ORDER_USE) {
                ExceptionOrderDao.getInstance().updateOrderPayState(portNum, Common.ORDER_PAYED, "");
            } else {
                TempOrderDao.getInstance().updateOrderPayState(portNum, Common.ORDER_PAYED);

            }
        }
    }
}
